﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeachLite.Entity;
using PeachLite.Helpers;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace PeachLite.Admin
{
    public abstract class DataPage : PageBase
    {
        protected abstract string Sql { get; }

        public string QueryString = "";

        protected virtual string[] FormatString
        {
            get
            {
                return null;
            }
        }

        protected override void Do()
        {
            base.Do();

            Control c;

            Literal ltHtml;
            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Capacity = 1024;

            if (Sql == "")
            {
                return;
            }

            SysResult r = SqlHelper.Execute(Sql);
            if (r.Success)
            {
                //设置页面标题
                Page.Title = r.Data["Title"].ToString() + Page.Title;

                //查找父控件
                var cph = Page.Master.FindControl("cphMain");
                //嵌套母版页查找
                if (cph == null)
                {
                    cph = Page.Master.Master.FindControl("cphMain").FindControl("cphMain");
                }
                //1.先进行单行数据内容html替换
                //校验是否需要pager
                if (r.Data["PSize"] != null && r.Data["PCount"] != null && r.Data["PIndex"] != null)
                {
                    c = cph.FindControl("Pager");
                    if (c != null)
                    {
                        ltHtml = (Literal)c;
                        ltHtml.Text = UIHelper.GetPager(int.Parse(r.Data["PSize"].ToString()), int.Parse(r.Data["PIndex"].ToString()), int.Parse(r.Data["PCount"].ToString()), 3, QueryString);
                    }
                }
                foreach (DictionaryEntry de in r.Data)
                {
                    c = cph.FindControl(de.Key.ToString());
                    if (c != null)
                    {
                        ltHtml = (Literal)c;
                        ltHtml.Text = de.Value == null ? "" : de.Value.ToString();
                    }
                }

                //2.再进行列表行数据的html替换
                for (int i = 0; i < r.DSet.Tables.Count; i++)
                {
                    // 查找ltProListX控件加入html
                    c = cph.FindControl("lt" + i.ToString());
                    if (c != null)
                    {
                        ltHtml = (Literal)c;
                        for (int j = 0; j < r.DSet.Tables[i].Rows.Count; j++)
                        {
                            string[] args = new string[r.DSet.Tables[i].Columns.Count];
                            for (int k = 0; k < r.DSet.Tables[i].Columns.Count; k++)
                            {
                                args[k] = r.DSet.Tables[i].Rows[j][k] == null ? "" : r.DSet.Tables[i].Rows[j][k].ToString();
                            }
                            sbHtml.Append(FormatString.Length >= i && !string.IsNullOrWhiteSpace(FormatString[i]) ? string.Format(FormatString[i], args) : args[0]);
                        }
                        ltHtml.Text = sbHtml.ToString();
                    }
                    sbHtml.Clear();
                }
            }
        }
    }
}